package com.jdd.modules.parking.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.common.constant.CacheConstant;
import com.jdd.common.util.CacheUtil;
import com.jdd.modules.parking.entity.ConfigParkInfo;
import com.jdd.modules.parking.mapper.ConfigParkInfoMapper;
import com.jdd.modules.parking.service.IConfigParkInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 车场临时车、月租车过期配置
 * @Author: jdd
 * @Date: 2021-02-18
 * @Version: V1.0
 */
@Service
public class ConfigParkInfoServiceImpl extends ServiceImpl<ConfigParkInfoMapper, ConfigParkInfo> implements IConfigParkInfoService {
    @Resource
    private ConfigParkInfoMapper configParkInfoMapper;
    @Resource
    CacheUtil cacheUtil;

    /**
     * 根据车场编码获取车场开闸信息配置
     *
     * @return
     */
    @Override
    public ConfigParkInfo findConfigParkInfoByParkCode() {
        LambdaQueryWrapper<ConfigParkInfo> queryWrapper = new LambdaQueryWrapper<>();
        String str = cacheUtil.get("ConfigParkInfo", "getConfigParkInfo");
        if (StringUtils.isBlank(str)) {
            ConfigParkInfo configParkInfo = this.getOne(queryWrapper);
            cacheUtil.put("ConfigParkInfo", "getConfigParkInfo", JSONObject.toJSONString(configParkInfo));
            return configParkInfo;
        }
        return JSON.parseObject(str, ConfigParkInfo.class);
    }

    /**
     * 根据开闸方式规则id获取对应的开闸方式规则
     *
     * @param id 计费规则id
     * @return 计费规则
     */
    @Override
    @Cacheable(value = CacheConstant.PARK_CONFIG_INFO_BY_ID, key = "#id", unless = "#result==null")
    public ConfigParkInfo findConfigParkInfoById(String id) {
        return this.getById(id);
    }

    /**
     * 删除对应车场编号的车场下的所有的开闸方式规则
     *
     * @return 删除结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CONFIG_INFO, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_INFO_BY_ID, allEntries = true)})
    public Boolean removeByParkCode() {
        cacheUtil.delete("ConfigParkInfo", "getConfigParkInfo");

        LambdaQueryWrapper<ConfigParkInfo> queryWrapper = new LambdaQueryWrapper<>();
        return this.remove(queryWrapper);
    }

    /**
     * 更新开闸方式规则实体
     *
     * @param configParkInfo 计费规则
     * @return 更新状态
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CONFIG_INFO, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_INFO_BY_ID, allEntries = true)})
    public boolean updateEntity(ConfigParkInfo configParkInfo) {
        if (configParkInfoMapper.updateById(configParkInfo) == 1) {
            cacheUtil.delete("ConfigParkInfo", "getConfigParkInfo");
            return true;
        }
        return false;
    }

    /**
     * 保存开闸方式规则实体
     *
     * @param configParkInfo 计费规则
     * @return 保存状态
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CONFIG_INFO, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_INFO_BY_ID, allEntries = true)})
    public boolean saveEntity(ConfigParkInfo configParkInfo) {
        if (configParkInfoMapper.insert(configParkInfo) == 1) {
            cacheUtil.delete("ConfigParkInfo", "getConfigParkInfo");
            return true;
        }
        return false;
    }

    /**
     * 删除开闸方式规则实体
     *
     * @param id 计费规则id
     * @return 删除状态
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CONFIG_INFO, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_INFO_BY_ID, allEntries = true)})
    public boolean deleteEntityById(String id) {
        if (configParkInfoMapper.deleteById(id) == 1) {
            cacheUtil.delete("ConfigParkInfo", "getConfigParkInfo");
            return true;
        }
        return false;
    }

    /**
     * 批量删除开闸方式规则实体
     *
     * @param ids 计费规则id列表
     * @return 删除状态
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CONFIG_INFO, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_INFO_BY_ID, allEntries = true)})
    public boolean deleteEntityByIds(List<String> ids) {
        if (configParkInfoMapper.deleteBatchIds(ids) == 1) {
            cacheUtil.delete("ConfigParkInfo", "getConfigParkInfo");
            return true;
        }
        return false;
    }

    /**
     * 清空表数据
     *
     * @return true 清空成功
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CONFIG_INFO, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_INFO_BY_ID, allEntries = true)})
    public boolean removeAllInfo() {
        try {
            cacheUtil.delete("ConfigParkInfo", "getConfigParkInfo");
            configParkInfoMapper.removeAllInfo();
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
